package com.bidevalution.business.job;

import com.bidevalution.business.factory.StepJobJdbcFactory;
import com.bidevalution.business.service.JdbcTemplateService;
import com.bidevalution.dao.entity.BeSectionStepJob;
import com.qtp.core.util.OConvertUtils;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.Hashtable;
import java.util.List;
import java.util.Map;

/**
 * @author huyiqi
 * @date 2020/3/23
 */
@Service("unionEntNameJob")
public class UnionEntNameJob implements StepJobJdbcFactory {
    private JdbcTemplateService jdbcTemplateService;
    @Override
    public Boolean execute(BeSectionStepJob beSectionStepJob, String submitCode, String expertId, String layoutCode) throws Exception {
        String sectionId = beSectionStepJob.getSectionId();
        Map<String, Object> sectionInfo = getSectionInfo(sectionId);
        if(sectionId == null) {
            throw new Exception("没有找到项目信息");
        }
        String sectionCode = OConvertUtils.getString(sectionInfo.get("sectionCode"));
        //获取联合体单位
        List<Map<String, Object>> unionEnts = getUnionEnts(sectionCode);
        if(unionEnts!=null && !unionEnts.isEmpty()) {
            for(Map<String, Object> ent:unionEnts) {
                String entName = OConvertUtils.getString(ent.get("EnterpriseName"));
                entName = entName.substring(1, entName.length()-1);
                List<String> entNames = Arrays.asList(entName.split("、"));
                //获取单位表中的单位信息
                Map<String, Object> enterpriseInfo = getEnterpriseInfo(sectionId, entNames);
                if(enterpriseInfo==null) {
                    continue;
                }
                String enterpriseId = OConvertUtils.getString(enterpriseInfo.get("seq_id"));
                //更新联合体单位名称
                updateEntName(entName, enterpriseId);
            }
        }
        return null;
    }

    @Override
    public void setJdbcTemplateService(JdbcTemplateService jdbcTemplateService) {
        this.jdbcTemplateService = jdbcTemplateService;
    }

    /**
     * 获取项目信息
     * @param sectionId 标段主键
     * @return 返回项目信息
     */
    private Map<String, Object> getSectionInfo(String sectionId) {
        String sql = "select section_code as sectionCode from be_section_info where seq_id=:sectionId";
        Map<String, Object> jdbcParam = new Hashtable<>(2);
        jdbcParam.put("sectionId", sectionId);
        return jdbcTemplateService.selectForMap(sql, jdbcParam);
    }

    /**
     * 获取联合体的签到单位信息
     * @param sectionCode 标段主键
     * @return 返回联合体单位信息
     */
    private List<Map<String, Object>> getUnionEnts(String sectionCode) {
        String sql = "select * from be_enterprise_check where sectionCode=:sectionCode and ISCombo=1";
        Map<String, Object> jdbcParam = new Hashtable<>(2);
        jdbcParam.put("sectionCode", sectionCode);
        return jdbcTemplateService.selectForList(sql, jdbcParam);
    }

    /**
     * 获取所有联合体单位的名称
     * @param signGuid 签到单位id
     * @return 返回联合体单位名称列表
     */
    private List<String> getCombos(String signGuid) {
        String sql = "select a.EnterpriseName as entName from be_enterprise_check a where a.Guid=:EnterpriseSign" +
                    " union" +
                    " select b.EnterpriseName as entName from be_enterprise_check_union b left join be_enterprise_check c on b.EnterpriseSign=c.Guid" +
                    " where c.guid=:EnterpriseSign";
        Map<String, Object> jdbcParam = new Hashtable<>(2);
        jdbcParam.put("EnterpriseSign", signGuid);
        return jdbcTemplateService.selectForList(sql, jdbcParam, String.class);
    }

    /**
     * 获取投标单位信息
     * @param sectionId 标段主键
     * @param entNames 单位名称
     * @return 返回投标单位信息
     */
    private Map<String, Object> getEnterpriseInfo(String sectionId, List<String> entNames) {
        StringBuilder sql = new StringBuilder("select seq_id from be_section_enterprise where section_id=:sectionId and enterprise_name in(");
        for(String entName:entNames) {
            sql.append("'").append(entName).append("',");
        }

        sql.append("'') limit 1");
        Map<String, Object> jdbcParam = new Hashtable<>(2);
        jdbcParam.put("sectionId", sectionId);
        return jdbcTemplateService.selectForMap(sql.toString(), jdbcParam);
    }

    /**
     * 更新单位名称
     * @param entName 单位名称
     * @param enterpriseId 投标单位id
     */
    private void updateEntName(String entName, String enterpriseId) {
        String sql = "update td_bidinfo set biin_Name=:entName where enterprise_id=:enterpriseId";
        Map<String, Object> jdbcParam = new Hashtable<>(2);
        jdbcParam.put("entName", entName);
        jdbcParam.put("enterpriseId", enterpriseId);
        jdbcTemplateService.update(sql, jdbcParam);
    }
}
